package com.sunshine.wrongquestionset.activity.exercise

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.sunshine.wrongquestionset.room.entity.QuestionEntity
import kotlin.math.min

/**
 * @author sunshine
 * @date 2021/3/30
 */
class ExerciseFragmentAdapter(
    fragmentActivity: FragmentActivity,
    private val list: List<QuestionEntity>?
): FragmentStateAdapter(fragmentActivity) {

    private val exerciseCardFragment = ExerciseCardFragment()
    private var maxPager: Int = list?.size?.plus(2)?:2

    override fun getItemCount(): Int {
        return maxPager
    }

    //2021 04 13 修复因list为null而引起的crash
    override fun createFragment(position: Int): Fragment {
        return when (position) {
            //第一页题目整体说明
            0 -> {
                ExerciseInfoFragment()
            }
            //最后一页提交
            maxPager - 1 -> {
                exerciseCardFragment
            }

            else -> ExerciseFragment.newInstance(position, list!![position - 1].id)
        }
    }

    fun getExerciseCardFragment(): ExerciseCardFragment {
        return exerciseCardFragment
    }

}